﻿var titBook = titBook || {};
titBook.core = titBook.core || {};

/*globals Core, ko, require*/
(function (core) {
    var dataBindingFunction = function (dom, kendo) {
        var domElements = {};

        return {
            getDomElement: function (id) {
                return domElements[id];
            },
            addDomElement: function (id, domElement) {
                if (!domElements[id]) {
                    domElements[id] = [];
                }
                //make sure we dont add the same binding twice
                if (domElements[id].indexOf(domElement) === -1) {
                    domElements[id].push(domElement);
                }
            },
            applyBinding: function (moduleId, viewModel) {

                var i, arrayLength, elements = this.getDomElement(moduleId), errors = "", domElement, element;

                if (elements === null || elements === undefined) {
                    throw new Error("No mapping defined for: " + moduleId);
                }


                for (i = 0, arrayLength = elements.length; i < arrayLength; i++) {
                    domElement = elements[i];

                    if (domElement === undefined || domElement === null) {
                        errors += "Unmapped moduleId passed to bind: " + moduleId + '\n';
                    }

                    element = document.getElementById(domElement);
                    if (element === undefined || element === null) {
                        errors += "Undefined dom element passed to bind: " + domElement + '\n';
                    }
                    else {
                        kendo.bind(element, viewModel);
                    }
                }

                //throw any accumulated errors
                if (errors !== "") {
                    throw new Error(errors);
                }
            },
            removeBinding: function (moduleId) {

                var i, arrayLength, elements = this.getDomElement(moduleId), errors = "", domElement, element;

                if (elements === null || elements === undefined) {
                    throw new Error("No mapping defined for: " + moduleId);
                }

                for (i = 0, arrayLength = elements.length; i < arrayLength; i++) {
                    domElement = elements[i];

                    if (domElement === undefined || domElement === null) {
                        errors += "Unmapped moduleId passed to bind: " + moduleId + '\n';
                    }
                    else {
                        element = dom.getElementById(domElement);
                        //if the dom element no longer exists on the screen, we don't need to unbind it
                        if (element === undefined || element === null) { }
                        else {
                            //remove any bindings on the node
                            kendo.unbind(element);
                        }
                    }
                }

                //throw any accumulated errors
                if (errors !== "") {
                    throw new Error(errors);
                }
            }
        };
    };

    core.DataBinding = (dataBindingFunction)(document, kendo);
    

})(titBook.core);